#if !defined(__HTTP_PARSER_H)
#    define __HTTP_PARSER_H

#    include <httpBasic.h>
#    include <httpRequest.h>
#    include <iostream>
#    include <map>
#    include <sstream>
#    include <string>

namespace http {

    class HttpParser {
        enum parts {
            StartLine,
            Headers,
            HeadersEnd,
            Body,
            End
        };

        parts part;

    private:
        std::string buf;

        // StringMap   http;
        long        content_length;
        std::string body;
        HttpRequest request;

    private:
        // 格式化 map_key
        std::string format_key(std::string &str);
        const void  parserBody();
        
        // 解析 hex 字符
        short int hexChar2dec(char c);

        // 转化 hex 字符
        char dec2hexChar(short int n);

        // URL 转码
        std::string escapeURL(const std::string &URL);

        // URL 解码
        std::string deescapeURL(const std::string &URL);

    public:
        HttpParser();
        ~HttpParser();

        // 获取请求对象
        HttpRequest &getRequest() { return request; };

        // 解析http请求, 可重复调用
        // 返回http请求剩余的长度
        // 解析完成 返回0
        int         parser(char *msg, size_t msg_size = 2048);
        std::string operator[](std::string str);
    };
}  // namespace http

#endif  // __HTTP_PARSER_H
